package com.scx.controller.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scx.context.BaseContext;
import com.scx.entity.Blog;
import com.scx.result.Result;
import com.scx.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/blog")
@Api("C端-社交文化接口")
@Slf4j
public class BlogController {

    @Resource
    private BlogService blogService;

    @PostMapping
    @ApiOperation("新增笔记")
    public Result saveBlog(@RequestBody Blog blog) {
        log.info("新增笔记:{}", blog);
        return blogService.saveBlog(blog);
    }

    @GetMapping("{id}")
    @ApiOperation("查询笔记详情")
    public Result queryBlogById(@PathVariable Long id) {
        log.info("查询笔记详情:{}", id);
        return blogService.queryBlogById(id);
    }

    @PutMapping("/like/{id}")
    @ApiOperation("修改点赞数量")
    public Result likeBlog(@PathVariable("id") Long id) {
        log.info("修改点赞数量{}", id);
        return blogService.likeBlog(id);
    }

    @GetMapping("/likes/{id}")
    @ApiOperation("查看点赞排行")
    public Result queryLikesById(@PathVariable("id") Long id) {
        log.info("查看点赞排行：{}", id);
        return blogService.queryLikesById(id);
    }

    @GetMapping("/of/me")
    @ApiOperation("获取登录用户笔记")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        log.info("获取登录用户,{}", current);
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", BaseContext.getCurrentId()).page(new Page<>(current, 10));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.success(records);
    }

    @GetMapping("/hot")
    @ApiOperation("笔记分页查询")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        log.info("笔记分页查询");
        return blogService.queryHotBlog(current);
    }

    @GetMapping("/of/user")
    @ApiOperation("根据用户id获取笔记")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, 10));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.success(records);
    }

    @GetMapping("/of/follow")
    @ApiOperation("分页查询收件箱")
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max,
                                    @RequestParam(value = "offest", defaultValue = "0") Long offset) {
        log.info("分页查询收件箱：{}，{}", max, offset);
        return blogService.queryBlogOfFollow(max, offset);
    }
}
